<?php
/**
 * .php
 * @description 
 * @author Mervin Wong <mervin0502@163.com>
 * @date
 * @link   http://mervin.me/
 */

class Common_model extends CI_Model{
	public function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	/**
	 * 检测是否登录
	 * @param string $session_id
	 * @return bool
	 */
	public function is_login($session_id){
		$this->db->select('status');
		$this->db->from('sessions');
		$this->db->where('session_id', $session_id);
		$query = $this->db->get();
		if($query->num_rows > 0){
			$row = $query->row_array();
			if($row['status'] == 'yes'){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	public function get_one($field, $table, $conditions){
 		$this->db->select($field);
 		$this->db->from($table);
 		foreach($conditions as $k=>$v){
 			$this->db->where($k, $v);
 		}
 		$query = $this->db->get();
 		if($query->num_rows > 0){
 			return $query->row_array();
 		}else{
 			return false;
 		}
 	}
 	
 	public function get_all($field, $table, $conditions = '', $ffset, $limit, $order = ''){
 		$this->db->select($field);
 		$this->db->from($table);
 		if(!empty($conditions)){
 			foreach($conditions as $k=>$v){
 				$this->db->where($k, $v);
 			}
 		}
 		if(!empty($order)){
 			foreach($order as $k=>$v){
 				$this->db->order_by($k, $v);
 			}
 		}
 		$this->db->limit($limit, $ffset);
 		$query = $this->db->get();
 		if($query->num_rows > 0){
 			return $query->result_array();
 		}else{
 			return false;
 		}
 	}
 	
 	public function get_num($field, $table, $conditions){
 		$this->db->select();
 		$this->db->from($table);
 		if(!empty($conditions)){
 			foreach($conditions as $k=>$v){
 				$this->db->where($k, $v);
 			}
 		}
 		$query = $this->db->get();
 		return $query->num_rows;
 	}
 	
 	 	public function update($field, $table, $conditions){
 		foreach($conditions as $k=>$v){
 			$this->db->where($k, $v);
 		}
 		$this->db->update($table, $field);
 		if($this->db->affected_rows() > 0){
 			return true;
 		}else{
 			return false;
 		}
 	}
 	
 	public function delete($table, $conditions){
 		foreach($conditions as $k=>$v){
 			$this->db->where($k, $v);
 		}
 		$this->db->delete($table);
 	}
}